//$.getScript("http://192.168.56.102/connectionCall.js");
$.getScript("/AMRA-SCC/MobilityAccess/WebContent/Static/Javascript/connectionCall.js");

function callCreateUser() {
  //On annule l'action pr�vue pour la validation d'un formulaire
  //event.preventDefault();
  var role = document.getElementById('role').value;
  var passwd = document.getElementById('password').value;
  var accessRights = [];
  $('#checkboxes input[type="checkbox"]:checked').each(function () {
                accessRights.push($(this).val()); 
        }); // magic
  // call php function
   passwd = sha1(passwd);
  
  $.ajax({
	type: "POST",
//	url: "http://192.168.56.102/crud.php",
	url: "/Server/crud.php",
	data: {
		maFonction : 'createUser',
		params : {
			role : role,
			passwd : passwd,
			accessRights : accessRights	
		}
	},
	
	success: function(data){	
		  alert("The role '"+ role +"' was successfully created.");
	},
	
	error: function(msg, string) {
		alert("Error : " + string);
	}
  });
   
}

function callUpdateUser() {
  //On annule l'action pr�vue pour la validation d'un formulaire
  //event.preventDefault();
  var role = document.getElementById('role').value;
  var accessRights = [];
  $('#checkboxes input[type="checkbox"]:checked').each(function () {
                accessRights.push($(this).val()); 
        }); // magic
  // call php function
  
  $.ajax({
	type: "POST",
	url: "/Server/crud.php",
//	url: "http://192.168.56.102/crud.php",
	data: {
		maFonction : 'alterRoleRights',
		params : {
			role : role,
			accessRights : accessRights	
		}
	},
	
	success: function(data){
		
		alert("The role's access rights were successfully updated.");
		document.getElementById("CheckButton").style.display="block";
		document.getElementById("checkboxes").style.display="none";
		document.getElementById("EditButton").style.display="none";

	},
	
	error: function(msg, string) {
		alert("Error : " + string);
		document.getElementById("CheckButton").style.display="block";
		document.getElementById("checkboxes").style.display="none";
		document.getElementById("EditButton").style.display="none";
	}
  });
   
}

function callCheckUser() {
  //On annule l'action pr�vue pour la validation d'un formulaire
  //event.preventDefault();
  var role = document.getElementById('role').value;
  var accessRights = [];
  $('#checkboxes input[type="checkbox"]:checked').each(function () {
                accessRights.push($(this).val()); 
        }); // magic
  // call php function
  
  $.ajax({
	type: "POST",
	url: "/Server/crud.php",
//	url: "http://192.168.56.102/crud.php",
	data: {
		maFonction : 'getRoleRights',
		params : {
			role : role,
		}
	},
	
	success: function(data){
			if (data == "") {
				alert("Error : User non existent");
				return;
			}
			
			document.getElementById("CheckButton").style.display="none";
			document.getElementById("checkboxes").style.display="block";
			document.getElementById("EditButton").style.display="block";
			
			var droit = data.split("");
			var checkboxes = document.getElementsByName("accessRights[]");
			
			//TODO: iterate over the checkbox elements using jquery
			if(droit[0]=='1')
				$("#6").prop("checked", true).checkboxradio("refresh");
			else
				$("#6").prop("checked", false).checkboxradio("refresh");
			if(droit[1]=='1')
				$("#4").prop("checked", true).checkboxradio("refresh");
			else
				$("#4").prop("checked", false).checkboxradio("refresh");
			if(droit[2]=='1')
				$("#5").prop("checked", true).checkboxradio("refresh");
			else
				$("#5").prop("checked", false).checkboxradio("refresh");
			if(droit[3]=='1')
				$("#1").prop("checked", true).checkboxradio("refresh");
			else
				$("#1").prop("checked", false).checkboxradio("refresh");
			if(droit[4]=='1')
				$("#2").prop("checked", true).checkboxradio("refresh");
			else
				$("#2").prop("checked", false).checkboxradio("refresh");
			if(droit[5]=='1')
				$("#3").prop("checked", true).checkboxradio("refresh");
			else
				$("#3").prop("checked", false).checkboxradio("refresh");
	},
	
	error: function(msg, string) {
		alert("Error : " + string);
	}
  });
   
}

